<template>
  <el-dialog
    :title="formData.id ? '编辑供应商' : '新增供应商'"
    :model-value="visible"
    @update:model-value="$emit('update:visible', $event)"
    width="600px"
    :before-close="handleClose" draggable>
    
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px">
      
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="供应商编码" prop="suCode">
            <el-input
              v-model="formData.suCode"
              placeholder="请输入供应商编码"
              :disabled="!!formData.id" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="供应商名称" prop="suName">
            <el-input
              v-model="formData.suName"
              placeholder="请输入供应商名称" />
          </el-form-item>
        </el-col>
      </el-row>

      <el-form-item label="备注" prop="remaker">
        <el-input
          v-model="formData.remaker"
          type="textarea"
          :rows="3"
          placeholder="请输入备注信息"
          maxlength="500"
          show-word-limit />
      </el-form-item>
    </el-form>
    
    <template #footer>
      <el-button @click="handleClose">取消</el-button>
      <el-button type="primary" @click="handleSubmit" :loading="loading">
        {{ formData.id ? '更新' : '创建' }}
      </el-button>
    </template>
  </el-dialog>
</template>

<script setup>
import { ref, reactive, watch, nextTick } from 'vue'
import { ElMessage } from 'element-plus'
import { request } from '@/utils/request'

// 定义 props
const props = defineProps({
  visible: {
    type: Boolean,
    default: false
  },
  formData: {
    type: Object,
    default: () => ({})
  }
})

// 定义 emits
const emit = defineEmits(['update:visible', 'submit', 'close'])

// 响应式数据
const loading = ref(false)
const formRef = ref()

// 表单验证规则
const formRules = {
  suCode: [
    { required: true, message: '请输入供应商编码', trigger: 'blur' },
    { min: 1, max: 50, message: '供应商编码长度在 1 到 50 个字符', trigger: 'blur' }
  ],
  suName: [
    { required: true, message: '请输入供应商名称', trigger: 'blur' },
    { min: 1, max: 100, message: '供应商名称长度在 1 到 100 个字符', trigger: 'blur' }
  ]
}

// 监听 visible 变化，重置表单验证
watch(() => props.visible, (newVal) => {
  if (newVal) {
    nextTick(() => {
      if (formRef.value) {
        formRef.value.clearValidate()
      }
    })
  }
})

// 关闭弹窗
const handleClose = () => {
  emit('update:visible', false)
  emit('close')
}

// 提交表单
const handleSubmit = async () => {
  if (!formRef.value) return
  
  try {
    // 表单验证
    await formRef.value.validate()
    
    loading.value = true
    
    // 触发提交事件，传递表单数据
    emit('submit', { ...props.formData })
    
  } catch (error) {
    if (error.errors) {
      ElMessage.error('请完善表单信息')
    }
  } finally {
    loading.value = false
  }
}

// 暴露方法给父组件
defineExpose({
  setLoading: (value) => {
    loading.value = value
  }
})
</script>

<style scoped>
/* 弹窗样式优化 */
:deep(.el-dialog__body) {
  padding: 20px;
}

:deep(.el-form-item) {
  margin-bottom: 20px;
}
</style>